#
# @author Wójcik Karol <wojcikk@student.agh.edu.pl>
# @licence http://www.gnu.org/copyleft/gpl.html GNU/GPL
#

# Filters added to this controller will be run for all controllers in the application.
# Likewise, all the methods added will be available for all controllers.
require 'login_system'

class ApplicationController < ActionController::Base
    session :session_key => '_system-planowania-zajec-v2.1_session_id'
    include LoginSystem
    model :user
    layout 'standard'


class ActionView::Base
        def error_messages_for(object_name, options = {})
            options = options.symbolize_keys
            object = instance_variable_get("@#{object_name}")                
            unless object.errors.empty?
            content_tag("div",
                 content_tag(
                   options[:header_tag] || "h4",
                   "Wyst&#261;pi&#322;a nast&#281;puj&#261;ca ilo&#347;&#263; b&#322;&#281;d&#243;w zwi&#261;zanych z obiektem '#{object_name.to_s.gsub("_", " ")}': #{object.errors.count}"
                 ) +
                 content_tag("p", "Wyst&#261;pi&#322; problem z nast&#281;puj&#261;cymi polami") +
                 content_tag("ul", object.errors.full_messages.collect { |msg| content_tag("li", msg) }),
                 "id" => options[:id] || "errorExplanation", "class" => options[:class] || "errorExplanation"
               )              
            end 
        end
    end    
    
    class ActiveRecord::Errors        
        def full_messages
           full_messages = [] 
           @errors.each_key do |attr|
             @errors[attr].each do |msg|
                   next if msg.nil?
                   if attr == "base"
                     full_messages << msg
                   else
                     full_messages << msg
                   end
             end
           end
           return full_messages
         end
    end  
end